package cc.opcol.iot.app.web;

import cc.opcol.iot.server.server.IoTSession;
import cc.opcol.iot.server.server.SessionManager;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/api/dev")
public class DeviceWeb {

    Gson gson = new Gson();

    @RequestMapping(value = "/cache/data", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String query(String key,String did) {
        IoTSession ioTSession=null;
        if(did.equals("-1")){
            ioTSession=SessionManager.getTopOne();
        }else {
            ioTSession=SessionManager.getByBizId(did);
        }
        Map<String,Object> data=new HashMap();
        if(ioTSession!=null) {
            if (!StringUtils.isBlank(key)) {
                for (String filed : key.split(",")) {
                    data.put(filed,ioTSession.getCache().get(filed));
                }
            }
        }
        return gson.toJson(new ResourceVo(data));
    }
}
